﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace PuanYonetimi
{
    public partial class frmMagaza : DevExpress.XtraEditors.XtraForm
    {
        public frmMagaza()
        {
            InitializeComponent();
        }

        private void Liste()
        {
            using (PromoEntities ent = new PromoEntities())
            {
                gridControl1.DataSource = ent.Magazalar.Select(x => new { MagazaKodu = x.Kodu, MagazaAdi = x.Adi, x.PosgenelYol, x.PoskonYol }).ToList();
                gridView1.BestFitColumns();
            }
            Clear();
        }


        private void frmMagaza_Load(object sender, EventArgs e)
        {
            Liste();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            using (PromoEntities ent = new PromoEntities())
            {
                var mg = ent.Magazalar.Where(x => x.Kodu == txtKod.Text).FirstOrDefault();
                if (mg != null)
                {
                    mg.Kodu = txtKod.Text;
                    mg.Adi = txtAdi.Text;
                    mg.PosgenelYol = txtPosgenel.Text;
                    mg.PoskonYol = txtPoskon.Text;
                }
                else
                {
                    mg = new Magaza();
                    mg.Kodu = txtKod.Text;
                    mg.Adi = txtAdi.Text;
                    mg.PosgenelYol = txtPosgenel.Text;
                    mg.PoskonYol = txtPoskon.Text;
                    ent.AddToMagazalar(mg);
                }
                ent.SaveChanges();
            }
            Liste();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            using (PromoEntities ent = new PromoEntities())
            {
                var mg = ent.Magazalar.Where(x => x.Kodu == txtKod.Text).FirstOrDefault();
                if (mg != null)
                {
                    ent.DeleteObject(mg);
                    ent.SaveChanges();
                }
            }
            Liste();
        }

        private void Clear()
        {
            txtKod.Text = "";
            txtAdi.Text = "";
            txtPosgenel.Text = "";
            txtPoskon.Text = "";
        }
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle > -1)
            {
                txtKod.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["MagazaKodu"]);
                txtAdi.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["MagazaAdi"]);
                txtPosgenel.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["PosgenelYol"]);
                txtPoskon.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns["PoskonYol"]);
            }
        }

        private void txtPosgenel_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtPosgenel.Text = dlg.SelectedPath;
            }
        }

        private void txtPoskon_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtPoskon.Text = dlg.SelectedPath;
            }
        }
    }
}